package configuration

import (
	"encoding/json"
	"errors"
	"fmt"
	"kop/modules/configuration/client"
	"kop/pb"
	"kop/util/log"
)

func init() {

	var err error
	var b = client.Load("TreasureTemplate")
	if err = json.Unmarshal(b, &TreasureConfigs); err != nil {
		panic(err)
	}

	log.Info("Load TreasureTemplate.json success.")

	//********************************************************************//

	b = client.Load("TreasureCell")
	if err = json.Unmarshal(b, &CellConfigs); err != nil {
		panic(err)
	}

	log.Info("Load TreasureCell.json success.")

	//********************************************************************//

}

type (
	// 宝藏
	TreasureConf struct {
		MapID        int32   `json:"ID"` // 地图ID
		Power        int64   // 推荐实力
		CostCoin     int64   // 探索消耗
		BoxAwardID   []int32 // 宝箱奖励ID
		EliteID      []int32 // 精英ID
		EliteLevel   int32   // 精英等级
		EliteAwardID int32   // 精英奖励ID
		BossID       int32   // 首领ID
		BossLevel    int32   // 首领等级
		BossAwardID  int32   // 首领奖励ID
		AwardID      int32   // 通关奖励ID
	}

	// 格子配置
	CellConf struct {
		ID       int32        // 格子类型ID
		Type     pb.TCellType // 格子类型
		Score    int32        // 得分
		CountMin int32        // 最少数量
		CountMax int32        // 最多数量
	}
)

var (
	TreasureConfigs = map[int32]TreasureConf{}
	CellConfigs     = map[int32]CellConf{}
)

func GetTreasureByMapID(MapID int32) (TreasureConf, error) {

	var conf, ok = TreasureConfigs[MapID]
	if ok {
		return conf, nil
	}

	return TreasureConf{}, errors.New(fmt.Sprintf("MapID not found [%d]", MapID))
}

func GetCellPoint(Type pb.TCellType) int32 {
	for _, v := range CellConfigs {
		if v.Type == Type {
			return v.Score
		}
	}
	return 0
}
